﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SalonPay.Entity;
using SalonPay.IBLL;
public partial class receive99bill : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string merchantAcctId = Request.QueryString["merchantAcctId"].ToString().Trim();
        string key = ConfigurationManager.AppSettings["99Bill_key"];
        string version = Request.QueryString["version"].ToString().Trim();
        string language = Request.QueryString["language"].ToString().Trim();
        string signType = Request.QueryString["signType"].ToString().Trim();
        string payType = Request.QueryString["payType"].ToString().Trim();
        string bankId = Request.QueryString["bankId"].ToString().Trim();
        string orderId = Request.QueryString["orderId"].ToString().Trim();
        string orderTime = Request.QueryString["orderTime"].ToString().Trim();
        string orderAmount = Request.QueryString["orderAmount"].ToString().Trim();
        string dealId = Request.QueryString["dealId"].ToString().Trim();
        string bankDealId = Request.QueryString["bankDealId"].ToString().Trim();
        string dealTime = Request.QueryString["dealTime"].ToString().Trim();
        string payAmount = Request.QueryString["payAmount"].ToString().Trim();
        string fee = Request.QueryString["fee"].ToString().Trim();

        //获取处理结果,10代表 成功; 11代表 失败
        string payResult = Request.QueryString["payResult"].ToString().Trim();
        //获取错误代码
        string errCode = Request.QueryString["errCode"].ToString().Trim();
        //获取加密签名串
        string signMsg = Request.QueryString["signMsg"].ToString().Trim();


        int rtnOk = 0;
        string rtnUrl = "";
        IOrder orderOpt = RemotingHelper.OrderService();


        #region 接收日志

        tb_PayReceiveLog log = new tb_PayReceiveLog();
        log.LBgUrl = "";
        log.LErrCode = errCode;
        log.LMerchantAccId = merchantAcctId;
        log.LOrderAmount = Convert.ToInt32(orderAmount);
        log.LOrderId = orderId;
        log.LOrderTime = common.StrToDateTime(orderTime);//"20100129123240 -> 2010-01-29 13:32:40"
        log.LPayBankDealId = bankDealId;
        log.LPayBankId = "";
        log.LPayGateWayDealDate = common.StrToDateTime(dealTime);
        log.LPayGateWayDealId = dealId;
        log.LPayGateWayFee = Convert.ToInt32(fee);
        log.LPayGateWayType = Convert.ToByte("1");
        log.LPayResult = payResult;
        log.LPayType = payType;

        orderOpt.AddReceiveLog(log);

        #endregion

        //生成加密串。必须保持如下顺序。
        string merchantSignMsgVal = "";
        merchantSignMsgVal = common.appendParam(merchantSignMsgVal, "merchantAcctId", merchantAcctId);
        merchantSignMsgVal = common.appendParam(merchantSignMsgVal, "version", version);
        merchantSignMsgVal = common.appendParam(merchantSignMsgVal, "language", language);
        merchantSignMsgVal = common.appendParam(merchantSignMsgVal, "signType", signType);
        merchantSignMsgVal = common.appendParam(merchantSignMsgVal, "payType", payType);
        merchantSignMsgVal = common.appendParam(merchantSignMsgVal, "bankId", bankId);
        merchantSignMsgVal = common.appendParam(merchantSignMsgVal, "orderId", orderId);
        merchantSignMsgVal = common.appendParam(merchantSignMsgVal, "orderTime", orderTime);
        merchantSignMsgVal = common.appendParam(merchantSignMsgVal, "orderAmount", orderAmount);
        merchantSignMsgVal = common.appendParam(merchantSignMsgVal, "dealId", dealId);
        merchantSignMsgVal = common.appendParam(merchantSignMsgVal, "bankDealId", bankDealId);
        merchantSignMsgVal = common.appendParam(merchantSignMsgVal, "dealTime", dealTime);
        merchantSignMsgVal = common.appendParam(merchantSignMsgVal, "payAmount", payAmount);
        merchantSignMsgVal = common.appendParam(merchantSignMsgVal, "fee", fee);
        merchantSignMsgVal = common.appendParam(merchantSignMsgVal, "payResult", payResult);
        merchantSignMsgVal = common.appendParam(merchantSignMsgVal, "errCode", errCode);
        merchantSignMsgVal = common.appendParam(merchantSignMsgVal, "key", key);

        string merchantSignMsg = common.GetMD5(merchantSignMsgVal, "utf-8");


        //商家进行数据处理，并跳转会商家显示支付结果的页面
        //首先进行签名字符串验证
        #region 
        if (signMsg.ToUpper() == merchantSignMsg.ToUpper())
        {
            switch (payResult)
            {
                case "10":
                    /*  
                     商户网站逻辑处理，比方更新订单支付状态为成功
                     特别注意：只有signMsg.ToUpper() == merchantSignMsg.ToUpper()，且payResult=10，才表示支付成功！同时将订单金额与提交订单前的订单金额进行对比校验。
                    */
                    
                    tb_Order orderInfo = new tb_Order();
                    orderInfo = orderOpt.GetOrderById(orderId);

                    
                    if (orderInfo != null && orderInfo.OStatus == 1 && orderInfo.OAmount == int.Parse(orderAmount))
                    {
                        //订单存在且未支付且金额相符时,更新订单状态为已支付(2)
                        bool payed = orderOpt.UpdateOrderStatus_Payed(orderInfo.OrderID);

                        if (payed)//支付成功，充值到游戏服务器
                        {
                            rtnOk = 1;
                            rtnUrl = ConfigurationManager.AppSettings["99Bill_showUrl"] + "?msg=success";

                            string charge_msg = orderOpt.Charge(orderInfo);//进行充值

                            tb_ChargeLog chargelog = new tb_ChargeLog();
                            chargelog.LOrderId = orderInfo.OrderID;
                            chargelog.LOrderAmount = orderInfo.OAmount;
                            
                            chargelog.LUserName = orderInfo.OUserName;
                            chargelog.LChargeType = "WEB";
                            chargelog.LChargeResult = charge_msg;

                            if (charge_msg!=null && charge_msg.Split(new char[1]{'|'},StringSplitOptions.None)[0]=="true")//充值成功
                            {
                                //更新订单状态为已充值(3)
                                orderOpt.UpdateOrderStatus_Charged(orderInfo.OrderID);

                                chargelog.LChargeStatus = 3; //充值成功 
                            }
                            else//充值失败
                            {
                                //更新订单状态为充值失败(4)                                
                                orderOpt.UpdateOrderStatus_ChargeFailed(orderInfo.OrderID);

                                chargelog.LChargeStatus = 4; //充值失败
                            }

                            orderOpt.AddChargeLog(chargelog);

                        }
                        else
                        {
                            rtnOk = 0;
                            rtnUrl = ConfigurationManager.AppSettings["99Bill_showUrl"] + "?msg=false";
                        }
                    }
                    else
                    {
                        rtnOk = 0;
                        rtnUrl = ConfigurationManager.AppSettings["99Bill_showUrl"] + "?msg=false";
                    }
                    
                    break;

                case "11":
                    rtnOk = 0;
                    rtnUrl = ConfigurationManager.AppSettings["99Bill_showUrl"] + "?msg=false";
                    break;
            }

        }
        else
        {
            //签名错误
            rtnOk = 0;
            rtnUrl = rtnUrl = ConfigurationManager.AppSettings["99Bill_showUrl"] + "?msg=false";

        }
        #endregion

        Response.Write("<result>"+ rtnOk +"</result><redirecturl>"+ rtnUrl +"</redirecturl>");

    }

}
